package com.wuwei.jy.Service;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Properties;

import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.stereotype.Component;

import com.wuwei.jy.domain.CommenData;

@Component
public class CommonDataUtil {

	private static ClassPathXmlApplicationContext appContext;

	public static ClassPathXmlApplicationContext getAppContext() {
		if (appContext == null) {
			appContext = new ClassPathXmlApplicationContext(new String[] { "classpath:commonData.xml" });

		}
		return appContext;
	}

	private List<CommenData> cdlist;

	public static CommenData Get(String key) {
		CommonDataUtil util = getAppContext().getBean("CommenDataUtil", CommonDataUtil.class);

		for (CommenData item : util.getCdlist()) {
			if (item.getKey().equals(key)) {
				return item;

			}
		}
		return null;
	}

	public static List<CommenData> GetList(String type) {
		CommonDataUtil util = getAppContext().getBean("CommenDataUtil", CommonDataUtil.class);

		List<CommenData> lst = new ArrayList<CommenData>();
		for (CommenData item : util.getCdlist()) {
			if (item.getType().equals(type)) {
				lst.add(item);

			}
		}
		return lst;

	}

	public List<CommenData> getCdlist() {
		return cdlist;
	}

	public void setCdlist(List<CommenData> cdlist) {
		this.cdlist = cdlist;
	}

	public static String GetPropertiy(String key) {

		Properties pro = new Properties();
		FileInputStream in;
		try {
			String path = CommonDataUtil.class.getResource("/resources/").getPath().replaceFirst("/", "") + "common.properties";
			System.out.println(path);
			in = new FileInputStream(path);

			try {
				pro.load(in);
				in.close();
			} catch (IOException e) {

				e.printStackTrace();
			}

		} catch (FileNotFoundException e) {

			e.printStackTrace();
		}

		return pro.getProperty(key);
	}

}
